package com.hnvec.androiduiclassone9;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public static final  String TAG="MainActivity";
    Button bn_bj,bn_gz,bn_sh;
    TextView mTv;
    private  List<Weather> weathers;







    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();

        String json1="{\"name\":\"zhangsan\",\"age\":27,\"married\":\"true\"}";
        Gson gson=new Gson();
        Person person=gson.fromJson(json1,Person.class);
        Log.e(TAG, "onCreate: "+person.getName() );
        String json2="[16,2,26]";
        Type listType=new TypeToken<List<Integer>>(){}.getType();
        List<Integer> ages=gson.fromJson(json2,listType);
        Log.e(TAG, "onCreate: "+ages.get(2) );
         String json3="[{\"temp\":\"20℃/30℃\",\"weather\":\"晴转多云\",\"name\":\"上海\",\"pm\":\"80\",\"wind\":\"1级\"},\n" +
                 "  {\"temp\":\"15℃/24℃\",\"weather\":\"晴\",\"name\":\"北京\",\"pm\":\"98\",\"wind\":\"3级\"},\n" +
                 "  {\"temp\":\"26℃/32℃\",\"weather\":\"多云\",\"name\":\"广州\",\"pm\":\"30\",\"wind\":\"2级\"}]";
         listType =new TypeToken<List<Weather>>(){}.getType();
        weathers=gson.fromJson(json3,listType);
        Log.e(TAG, "onCreate: "+weathers.get(2).getWeather() );


    }

    private void initEvent() {
        bn_gz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Weather weath=weathers.get(1);
                mTv.setText(weath.getName());

            }
        });
        bn_bj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Weather weath=weathers.get(2);
                mTv.setText(weath.getName());

            }
        });}


    private void initView() {
        bn_bj=findViewById(R.id.btn_bj);
        bn_gz=findViewById(R.id.btn_gz);
        bn_sh=findViewById(R.id.btn_sh);
        mTv=findViewById(R.id.tv_name);
    }
}
